package com.szholly.plug.form.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Component;

import com.szholly.utils.freemarker.directive.DirectiveUtils;
import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;
import com.szholly.utils.util.PinYinUtil;
import com.szholly.utils.util.StringUtils;
import com.szholly.utils.util.TreeDataNode;
import com.szholly.data.general.IDomain;
import com.szholly.data.general.ITableFactory;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 定义Combox下拉框的字典数据自定义标签
 */
@Component("directive_domain")
public class DomainDirective implements TemplateDirectiveModel, IDirective {

	/**
	 * 定义字典表名称
	 */
	private static String PARA_TABLENAME = "tableName";
	/**
	 * 定义字典域名称
	 */
	private static String PARA_DOMAIN = "domainName";
	/**
	 * 是否添加空行
	 */
	private static String PARA_ADDSPACEITEM = "addSpaceItem";
	
	private static String PARA_ADDPINYIN = "addPinYin";

	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "tableName：数据字典表名 <br/>domainName：字典域名称(可为空)<br/>addSpaceItem：是否添加空行(true,false)"
				+ "调用示例：<br/>"
				+ "<Combobox <br/>"
				+ "data=<@domain addSpaceItem='true' tableName='FS_FORM_FORM' domainName='FLP_NAME'>${data}</@domain> <br/>"
				+ "/><br/>";
		FunctionInfo info = new FunctionInfo("domain", 1, "表单",
				"获取下拉框的字典数据(json字符串)", paraInfo);
		return info;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		String tableName = DirectiveUtils.getString(PARA_TABLENAME, params);
		String domianName = DirectiveUtils.getString(PARA_DOMAIN, params);
		Boolean addSpaceItem = DirectiveUtils
				.getBool(PARA_ADDSPACEITEM, params);
		Boolean addPinYin = DirectiveUtils.getBool(PARA_ADDPINYIN, params);

		String returnValue = null;
		if (StringUtils.IsNullOrSpace(tableName)) {
			returnValue = "";
		}else{
			tableName = tableName.toUpperCase();
			ITableFactory tableFactory = (ITableFactory) SpringBeanFactory
					.getBean("TableFactory");
			IDomain pTable = tableFactory.openDomain(tableName, domianName);
			List<TreeDataNode> result = null;
			if(pTable!=null){
				result = pTable.getDatasEx();
			}else{
				result = new ArrayList<TreeDataNode>();
			}
			if(addPinYin != null && addPinYin == true){
				for (TreeDataNode node : result) {
					String text = node.getText();
					String[] headPy = PinYinUtil.getHeadByString(text,false);
					node.setText_py_(PinYinUtil.stringArrayToString(headPy));
					node.setText_fullPy_(PinYinUtil.hanziToPinyin(text, ""));
				}
			}
			if (addSpaceItem != null && addSpaceItem == true) {
				TreeDataNode spaceNode = new TreeDataNode();
				spaceNode.setid("");
				spaceNode.setText("");
				spaceNode.setText_py_("");
				spaceNode.setText_fullPy_("");
				spaceNode.setState("open");
				if (result == null) {
					result = new ArrayList<TreeDataNode>();
				}
				result.add(0, spaceNode);
			}
			
			JSONArray arrayObject = JSONArray.fromObject(result);
			returnValue = arrayObject.toString();
		}

		Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
				params);
		paramWrap.put("data", DEFAULT_WRAPPER.wrap(returnValue));
		Map<String, TemplateModel> origMap = DirectiveUtils
				.addParamsToVariable(env, paramWrap);
		body.render(env.getOut());
		DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
	}
}